<?php
/**
 * Created by PhpStorm.
 * User: Luo Kai Ge
 * Date: 2019/9/16
 * Time: 11:27
 */

namespace App\Libraries\Common;

use Illuminate\Support\Facades\Redis;

class CommonFun
{
    /**
     * 创建赠送会员订单流水
     * @param string $unique
     * @param string $pre
     * @return string
     */
    public static function createTradeId($unique, $pre = 'ORD')
    {
        $date = date('Ymd');
        $number = Redis::connection()->incrby($date . $pre, 1);
        Redis::connection()->expire($date . $pre, (24 * 3600) + rand(1000, 2000));

        $count = strlen($number);
        $str = '';
        for ($i = 8; $i - $count >= 0; $i--) {
            $str .= '0';
        }

        return strtoupper($unique. date('ymdHis') . $pre . $str . $number);
    }

    public static function getOffset($page, $size = 20)
    {
        return ($page - 1) * $size;
    }

    public static function changeMoney($money)
    {
        return bcdiv($money, 100, 2);
    }

    public static function curl($url, $method = "POST", $data = array(), $header = array(), $head = 0, $body = 0, $connect_timeout = 8, $timeout = 8, &$error = '')
    {
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $url);
        if (strpos($url, "https") !== false) {
            curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
            curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
            if (isset($_SERVER['HTTP_USER_AGENT'])) {
                curl_setopt($ch, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']);
            }
        }
        if (!empty($header)) {
            curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
        }
        switch ($method) {
            case 'POST':
                if (is_array($data)) {
                    $data = http_build_query($data);
                }
                curl_setopt($ch, CURLOPT_POST, 1);
                curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
                break;
            case 'GET':
                break;
            case 'PUT':
                curl_setopt($ch, CURLOPT_PUT, 1);
                curl_setopt($ch, CURLOPT_INFILE, '');
                curl_setopt($ch, CURLOPT_INFILESIZE, 10);
                break;
            case 'DELETE':
                curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "DELETE");
                break;
            default:
                break;
        }

        curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);

        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_HEADER, $head);
        curl_setopt($ch, CURLOPT_NOBODY, $body);
        curl_setopt($ch,CURLINFO_HEADER_OUT,1);
        curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $connect_timeout);
        curl_setopt($ch, CURLOPT_TIMEOUT, $timeout);
        //获取请求头
        $rtn = curl_exec($ch); //获得返回

        if (curl_errno($ch)) {
            $error = curl_error($ch);
            $getinfo = curl_getinfo($ch);
            $data_info = @json_decode($data,true);
            $request_body = [
                'header' => $getinfo,
                'body' => $data_info ? $data_info : $data
            ];
            return false;
        }
        curl_close($ch);
        return $rtn;
    }

}